package cn.yikaojiuguo.student.ui.ask;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.adapter.SimpleBaseAdapter;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.Ask;
import cn.yikaojiuguo.student.bean.AskPlayingHolder;
import cn.yikaojiuguo.student.bean.AskReceive;
import cn.yikaojiuguo.student.bean.AskTeachers;
import cn.yikaojiuguo.student.bean.Member;
import cn.yikaojiuguo.student.bean.MessageEvent;
import cn.yikaojiuguo.student.bean.Picture;
import cn.yikaojiuguo.student.support.simple.ArgsConstants;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.utils.Util;
import cn.yikaojiuguo.student.support.utils.VeDate;
import cn.yikaojiuguo.student.support.utils.ViewUtils;
import cn.yikaojiuguo.student.support.volley.Response;
import cn.yikaojiuguo.student.ui.base.BaseImgsActivity;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;
import cn.yikaojiuguo.student.ui.view.BaseRfreshLayout;
import cn.yikaojiuguo.student.ui.view.ClickableSeekBar;

public class AskDetailActivity extends CustomerActionBarActivity implements View.OnClickListener,
        AskResultPopupWindow.OnItemClickListener, ChooseRefuseContentPopupWindow
                .OnItemClickListener {

    @Bind(R.id.mrfl)
    BaseRfreshLayout mRfl;
    @Bind(R.id.lv)
    ListView mLv;
    @Bind(R.id.ll_input)
    LinearLayout mLl_input;
    @Bind(R.id.et)
    EditText mEt_input;
    @Bind(R.id.btn_send)
    Button mBtn_send;
    @Bind(R.id.rl_root)
    RelativeLayout mRl_root;
    private List<Ask> mList = new ArrayList<Ask>();

    //刚刚发出提问，还没有老师抢答
    public final static int FIRST_ASK = 1;
    //老师抢到题目，正在解答
    public final static int RES0LVING = 2;
    //问答正在进行
    public final static int ASKING = 3;
    public final static int FINISH = 4;

    private View mFootView;
    private int mType = FINISH;
    private boolean mGet_header = false;
    private boolean mGet_reply = false;
    private AskTeachers mAskTeachers;
    private Chronometer chronometer;
    private int mGold = 0;
    private int mPreGold = 0;
    private ImageView iv_0;
    private ImageView iv_1;
    private ImageView iv_3;
    private Calendar mCalendar = Calendar.getInstance();
    private SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private MediaPlayer mPlayer = new MediaPlayer();
    private AskPlayingHolder mPlayingHolder;
    private Timer mTimer = new Timer();
    private boolean isSliding;
    private boolean isFirstComment = false;
    private Gson mGson = new Gson();
    private AskDetailAdapter mAdapter;
    private AskResultPopupWindow mResultPopupWindow;
    private ChooseRefuseContentPopupWindow mRefusePopupWindow;

    public static Intent newIntent() {
        Intent intent = new Intent(YiKaoApplication.getInstance(), AskDetailActivity.class);
        return intent;
    }

    public static Intent startAskInstance(AskTeachers askTeachers) {
        Intent intent = new Intent(YiKaoApplication.getInstance(), AskDetailActivity.class);
        intent.putExtra(ArgsConstants.BEAN, askTeachers);
        intent.putExtra(ArgsConstants.TYPE, FIRST_ASK);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask_detail);
        ButterKnife.bind(this);
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (mPlayingHolder.csb != null) {
                    String tag = (String) mPlayingHolder.csb.getTag();
                    //if (mPlayingHolder.voice_path.equals(tag)) {
                    mTimer.cancel();
                    mTimer = null;
                    mPlayingHolder.csb.setProgress(0f);
                    mPlayingHolder.csb.setSliderImgSrc(R.drawable.ask_voice_play);
                    mPlayingHolder = null;
                    //}
                }
            }
        });
        mType = getIntent().getIntExtra(ArgsConstants.TYPE, FINISH);
        initView();
        initEvents();
        getData();
    }

    private void initEvents() {
        mBtn_send.setOnClickListener(this);
        mResultPopupWindow.setOnItemClickListener(this);
        mRefusePopupWindow.setOnItemClickListener(this);
    }

    private void getData() {
        showProgressDialog("正在加载...");
        mGet_header = false;
        mGet_reply = false;
        switch (mType) {
            case FIRST_ASK:
                mAskTeachers = (AskTeachers) getIntent().getSerializableExtra(ArgsConstants.BEAN);
                getHeaderData();
                break;
            case RES0LVING:

                break;
            case ASKING:
                break;
            case FINISH:
                break;
        }
    }

    private void getHeaderData() {
        String headerUrl = UrlConstants.ASK_DETAIL_HEADER + "&id=" + mAskTeachers.askId;
        RequestUtil.requestGet(headerUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null && response.optBoolean("success")) {
                    mGet_header = true;
                    Gson gson = new Gson();
                    if (mGet_header && mGet_reply && mType != FIRST_ASK) { //加载完问答详情与回复列表
                    } else if (mType == FIRST_ASK) {//显示发送给老师
                        dismissProgressDialog();
                        Ask ask = gson.fromJson(response.optJSONObject("data").toString(), Ask
                                .class);
                        mList.clear();
                        mList.add(ask);
                        mList.add(new Ask(AskDetailAdapter.TYPE_ADD_GOLD));
                        LogUtil.showInfoLog("mList", "mList.size = " + mList.size());
                        mAdapter = new AskDetailAdapter(AskDetailActivity.this, mList);
                        mLv.setAdapter(mAdapter);
                    }
                }
            }
        }, null, mTag);
    }

    private void initView() {
        mFootView = View.inflate(this, R.layout.layout_footer_load_more, null);
        mLv.setFooterDividersEnabled(false);
        mRfl.setColorSchemeResources(R.color.background_login);
        mRfl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRfl.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRfl.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        mRfl.setCanLoadMore(false);
        mResultPopupWindow = new AskResultPopupWindow(this, 0, 0);
        mRefusePopupWindow = new ChooseRefuseContentPopupWindow(this, 0.8f, 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_0:
                resetGoldButton(0);
                addGold(0);
                break;
            case R.id.iv_1:
                resetGoldButton(1);
                addGold(1);
                break;
            case R.id.iv_3:
                resetGoldButton(3);
                addGold(3);
                break;
            case R.id.btn_send:
                sendComment();
                break;
            case R.id.iv_operation:
                mResultPopupWindow.showAsDropDown(mIv_operation, 0, -6);
                break;
        }
    }

    private void sendComment() {
        String content = mEt_input.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            return;
        }
        try {
            content = URLEncoder.encode(content, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String requestUrl = UrlConstants.ADD_COMMENT + "&mid=" + YiKaoApplication.getUserId() +
                "&askid=" + mAskTeachers.askId + "&content=" + content + "&etype=0";
        showProgressDialog("正在添加回复...");
        RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                dismissProgressDialog();
                if (response != null && response.optBoolean("success")) {
                    notifyCommentList();
                }
            }
        }, null, mTag);
    }

    private void addGold(int gold) {
        if (gold > mPreGold) {
            String requestUrl = UrlConstants.ADD_GOLD + "&mid=" + YiKaoApplication.getUserId() +
                    "&price=" + gold + "&askid=" + mAskTeachers.askId;
            RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if (response != null && response.optBoolean("success")) {
                        LogUtil.showInfoLog("add gold", "add gold success");
                    } else {
                        mGold = mPreGold;
                        resetGoldButton(mPreGold);
                    }
                }
            }, null, mTag);
        }
    }

    private void resetGoldButton(int gold) {
        if (gold < mGold) {
            ToastUtil.showToast(this, "金币不能小于当前追加的金币数");
            return;
        }
        mPreGold = mGold;
        mGold = gold;
        switch (gold) {
            case 0:
                iv_0.setImageResource(R.drawable.gold_selected);
                iv_1.setImageResource(R.drawable.gold_unselected);
                iv_3.setImageResource(R.drawable.gold_unselected);
                break;
            case 1:
                iv_0.setImageResource(R.drawable.gold_selected);
                iv_1.setImageResource(R.drawable.gold_selected);
                iv_3.setImageResource(R.drawable.gold_unselected);
                break;
            case 3:
                iv_0.setImageResource(R.drawable.gold_selected);
                iv_1.setImageResource(R.drawable.gold_selected);
                iv_3.setImageResource(R.drawable.gold_selected);
                break;
        }
    }

    @Override
    public void accept() {
        showProgressDialog("正在加载...");
        String requestUrl = UrlConstants.ASK_ACCEPT + "&mid=" + YiKaoApplication.getUserId() +
                "&askid=" + mAskTeachers.askId;
        RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                dismissProgressDialog();
                mResultPopupWindow.dismiss();
                if (response != null && response.optBoolean("success")) {
                    mIv_operation.setVisibility(View.GONE);
                    mLl_input.setVisibility(View.GONE);
                    mList.remove((mList.size() - 1));
                    mList.add(1, new Ask(AskDetailAdapter.TYPE_EVALUATE));
                    mAdapter.notifyDataSetChanged();
                    ToastUtil.showToast(AskDetailActivity.this, "采纳成功");
                }
            }
        }, null, mTag);
    }

    @Override
    public void refuse() {
        mResultPopupWindow.dismiss();
        showRefuseDialog();
    }

    private void showRefuseDialog() {
        mRefusePopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        mRefusePopupWindow.showAtLocation(mRl_root, Gravity.CENTER, 0, 0);
    }

    @Override
    public void onItemClick(String content) {
        mRefusePopupWindow.dismiss();
        showProgressDialog("正在加载");
        try {
            String requestUrl = UrlConstants.ASK_REFUSE + "&mid=" + YiKaoApplication.getUserId() +
                    "&askid=" + mAskTeachers.askId + "&content=" + URLEncoder.encode(content,
                    "UTF-8");
            RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    dismissProgressDialog();
                    if (response != null && response.optBoolean("success")) {
                        ToastUtil.showToast(AskDetailActivity.this, "拒绝成功");
                        mIv_operation.setVisibility(View.GONE);
                        mLl_input.setVisibility(View.GONE);
                        mList.remove((mList.size() - 1));
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }, null, mTag);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public class AskDetailAdapter extends SimpleBaseAdapter<Ask> {

        public final static int TYPE_ADD_GOLD = 0;
        public final static int TYPE_IMAGS = 1;
        public final static int TYPE_VOICE = 2;
        public final static int TYPE_WORD = 3;
        public final static int TYPE_RESOLVE = 4;
        public final static int TYPE_ACCEPT = 5;
        public final static int TYPE_EVALUATE = 6;

        public AskDetailAdapter(Context context, List<Ask> list) {
            super(context, list);
        }

        @Override
        public int getItemViewType(int position) {
            Ask ask = getItem(position);
            return ask.getType();
        }

        @Override
        public int getViewTypeCount() {
            return 7;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LogUtil.showInfoLog("getViewType", "type = " + getItemViewType(position));
            switch (getItemViewType(position)) {
                case TYPE_ADD_GOLD:
                    return getAddGoldView(convertView);
                case TYPE_IMAGS:
                    return getImgsView(position, convertView, parent);
                case TYPE_VOICE:
                    return getVoiceView(position, convertView, parent);
                case TYPE_WORD:
                    return getWordView(position, convertView, parent);
                case TYPE_RESOLVE:
                    return getResolveView(convertView);
                case TYPE_ACCEPT:
                    return getAcceptView(convertView);
                case TYPE_EVALUATE:
                    return getEvaluateView(convertView);
            }
            return convertView;
        }

        private View getEvaluateView(View convertView) {
            EvaluateViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_ask_detail_evaluate, null);
                holder = new EvaluateViewHolder();
                holder.ll_container = ViewUtils.find(convertView, R.id.ll_container);
                holder.iv_btn = ViewUtils.find(convertView, R.id.iv_btn);
                convertView.setTag(holder);
            } else {
                holder = (EvaluateViewHolder) convertView.getTag();
            }
            final EvaluateViewHolder finalHolder = holder;
            holder.iv_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int visibility = finalHolder.ll_container.getVisibility() == View.VISIBLE ?
                            View.GONE : View.VISIBLE;
                    finalHolder.ll_container.setVisibility(visibility);
                }
            });
            return convertView;
        }

        private View getAcceptView(View convertView) {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_ask_detail_accept, null);
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    accept();
                }
            });
            return convertView;
        }

        private View getResolveView(View convertView) {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_ask_detail_resolve, null);
            }
            return convertView;
        }

        private View getWordView(int position, View convertView, ViewGroup parent) {
            BaseViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_ask_detail_word, null);
                holder = new BaseViewHolder();
                findBaseHolderViews(holder, convertView);
                convertView.setTag(holder);
            } else {
                holder = (BaseViewHolder) convertView.getTag();
            }
            Ask ask = getItem(position);
            bindUserData(ask, holder, position);
            return convertView;
        }

        private View getVoiceView(int position, View convertView, ViewGroup parent) {
            VoiceViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_ask_detail_voice, null);
                holder = new VoiceViewHolder();
                findBaseHolderViews(holder, convertView);
                holder.csb = ViewUtils.find(convertView, R.id.csb);
                holder.tv_voice_time = ViewUtils.find(convertView, R.id.tv_voice_time);
                convertView.setTag(holder);
            } else {
                holder = (VoiceViewHolder) convertView.getTag();
            }
            final Ask ask = getItem(position);
            holder.csb.setTag(ask.video);
            if (mPlayingHolder != null && mPlayingHolder.voice_path.equals(ask.video)) {
                holder.csb.setSliderImgSrc(R.drawable.ask_voice_pause);
                if (mPlayer != null && mPlayer.isPlaying()) {
                    int duration = mPlayer.getDuration();
                    int currentPosition = mPlayer.getCurrentPosition();
                    mPlayingHolder.csb.setProgress(((float) currentPosition) /
                            duration);
                }
            } else {
                holder.csb.setSliderImgSrc(R.drawable.ask_voice_play);
                holder.csb.setProgress(0);
            }
            bindUserData(ask, holder, position);
            final VoiceViewHolder finalHolder = holder;
            //前方高能预警
            holder.csb.setOnSliderClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.showInfoLog(mTag, "onClick");
                    if (mPlayingHolder != null) {
                        //停止正在播放的音频
                        mPlayer.stop();
                        mTimer.cancel();
                        mTimer = null;
                        mPlayingHolder.csb.setSliderImgSrc(R.drawable.ask_voice_play);
                        if (!mPlayingHolder.voice_path.equals(ask.video)) {
                            //正在播放的条目不为当前条目，则把正在播放的条目进度置0
                            mPlayingHolder.csb.setProgress(0f);
                            mPlayingHolder.tv_voice_time.setText(Util.getFixedTime(Integer
                                    .parseInt(mPlayingHolder.voice_time)));
                            mPlayingHolder = null;
                            //播放当前条目
                            doPlay(finalHolder, ask);
                        } else {
                            mPlayingHolder = null;
                        }
                    } else {
                        //播放当前条目
                        doPlay(finalHolder, ask);
                    }

                }
            });
            holder.tv_voice_time.setText(Util.getFixedTime(Integer.parseInt(ask.audiotime)));
            if (mPlayingHolder != null) {
                LogUtil.showInfoLog("voice", position + " : " + mPlayingHolder.voice_path.equals
                        (ask.video));
            }
            return convertView;
        }

        private void doPlay(final VoiceViewHolder holder, final Ask ask) {
            holder.csb.setSliderImgSrc(R.drawable.ask_voice_pause);
            try {
                mPlayer.reset();
                mPlayer.setDataSource(mContext, Uri.parse(ask.video));
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mPlayer.prepare();
                mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        holder.csb.setOnSlideListener(new ClickableSeekBar
                                .OnSlideListener() {


                            @Override
                            public void onStartSlide(ClickableSeekBar csb) {
                                LogUtil.showInfoLog("seekto", "start slide");
                                isSliding = true;
                            }

                            @Override
                            public void onStopSlide(ClickableSeekBar csb) {
                                isSliding = false;
                                if (mPlayer.isPlaying() && ask.video.equals
                                        (mPlayingHolder.voice_path)) {
                                    LogUtil.showInfoLog("seekto", "seek to " + csb
                                            .getProgress());
                                    mPlayer.seekTo((int) (mPlayer.getDuration() * csb
                                            .getProgress()));
                                }
                            }
                        });
                        float progress = holder.csb.getProgress();
                        mPlayer.start();
                        mPlayer.seekTo((int) (mPlayer.getDuration() * progress));
                        mPlayingHolder = new AskPlayingHolder();
                        mPlayingHolder.csb = holder.csb;
                        mPlayingHolder.tv_voice_time = holder.tv_voice_time;
                        mPlayingHolder.voice_path = ask.video;
                        mPlayingHolder.voice_time = ask.audiotime;
                        if (mTimer == null) {
                            mTimer = new Timer();
                        }
                        mTimer.schedule(new VoiceTask(), 0, 100);
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private View getAddGoldView(View convertView) {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_ask_detail_teachers, null);
                chronometer = ViewUtils.find(convertView, R.id.chronometer);
                chronometer.start();
                SpannableString ss = new SpannableString("已发给" + mAskTeachers.teachers.size() +
                        "位老师，等待时间");
                ss.setSpan(new ForegroundColorSpan(getResources().getColor(android.R.color
                                .holo_red_light)), 3,
                        4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                TextView tv_tip = ViewUtils.find(convertView, R.id.tv_tip);
                tv_tip.setText(ss);
                LinearLayout ll_avatars = ViewUtils.find(convertView, R.id.ll_avatars);
                for (Member member : mAskTeachers.teachers) {
                    ImageView iv = new ImageView(mContext);
                    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Util
                            .dip2px(mContext, 60), Util.dip2px(mContext, 60));
                    iv.setPadding(10, 10, 10, 10);
                    iv.setLayoutParams(layoutParams);
                    ImageLoader.getInstance().displayImage(member.avatar, iv);
                    ll_avatars.addView(iv);
                }
                //金币
                iv_0 = ViewUtils.find(convertView, R.id.iv_0);
                iv_1 = ViewUtils.find(convertView, R.id.iv_1);
                iv_3 = ViewUtils.find(convertView, R.id.iv_3);
                iv_0.setOnClickListener(AskDetailActivity.this);
                iv_1.setOnClickListener(AskDetailActivity.this);
                iv_3.setOnClickListener(AskDetailActivity.this);
            }
            return convertView;
        }

        private View getImgsView(int position, View convertView, ViewGroup parent) {
            ImgsViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_ask_detail_imgs, null);
                holder = new ImgsViewHolder();
                findBaseHolderViews(holder, convertView);
                holder.gv = ViewUtils.find(convertView, R.id.gv);
                convertView.setTag(holder);
            } else {
                holder = (ImgsViewHolder) convertView.getTag();
            }
            Ask ask = getItem(position);
            bindUserData(ask, holder, position);
            ImgsAdapter adapter = new ImgsAdapter(mContext, ask.pictures);
            final ArrayList<String> pics = new ArrayList<String>(ask.pictures.size());
            for (Picture pic : ask.pictures) {
                LogUtil.showInfoLog("large pic", pic.pic);
                pics.add(pic.pic);
            }
            holder.gv.setAdapter(adapter);
            holder.gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    startActivity(BaseImgsActivity.newIntent(BaseImgsActivity.class, pics,
                            position, false));
                }
            });
            return convertView;
        }

        private void findBaseHolderViews(BaseViewHolder holder, View convertView) {
            //条目顶部用户信息
            holder.iv_avatar = ViewUtils.find(convertView, R.id.iv_avatar);
            holder.tv_name = ViewUtils.find(convertView, R.id.tv_name);
            holder.tv_province = ViewUtils.find(convertView, R.id.tv_province);
            holder.tv_school = ViewUtils.find(convertView, R.id.tv_school);
            holder.tv_time = ViewUtils.find(convertView, R.id.tv_time);
            holder.tv_content = ViewUtils.find(convertView, R.id.tv_content);
            holder.ll_root = ViewUtils.find(convertView, R.id.ll_root);
        }

        private void bindUserData(Ask ask, BaseViewHolder holder, int position) {
            ImageLoader.getInstance().displayImage(ask.member.avatar, holder.iv_avatar, new
                    ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    LogUtil.showInfoLog("avatar", "error because : " + failReason.toString());
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                }
            });
            holder.tv_name.setText(ask.member.username);
            try {
                mCalendar.setTime(mFormat.parse(ask.pubdate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            holder.tv_time.setText(VeDate.formatTimeDuration((long) (System.currentTimeMillis() -
                    mCalendar.getTimeInMillis())));
            if (ask.member.isteacher) {

            } else {
                holder.tv_province.setText(ask.member.provincename);
                holder.tv_school.setText(ask.member.gradename);
            }
            if (TextUtils.isEmpty(ask.content)) {
                holder.tv_content.setVisibility(View.GONE);
            } else {
                holder.tv_content.setText(ask.content);
            }
            if (position == 0) {
                holder.ll_root.setPadding(0, 0, 0, 0);
            } else {
                int padding = Util.dip2px(mContext, 10);
                holder.ll_root.setPadding(padding, padding, padding, padding);
            }
        }

        class ImgsAdapter extends BaseAdapter {

            private Context mContext;
            private List<Picture> mPictures;

            public ImgsAdapter(Context context, List<Picture> pictures) {
                this.mContext = context;
                this.mPictures = pictures;
            }

            @Override
            public int getCount() {
                return mPictures.size();
            }

            @Override
            public Picture getItem(int position) {
                return mPictures.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                GridViewHolder holder = null;
                if (convertView == null) {
                    convertView = View.inflate(mContext, R.layout.item_ask, null);
                    holder = new GridViewHolder();
                    holder.iv = ViewUtils.find(convertView, R.id.iv);
                    convertView.setTag(holder);
                } else {
                    holder = (GridViewHolder) convertView.getTag();
                }
                ImageLoader.getInstance().displayImage(getItem(position).thumbpic, holder.iv);
                return convertView;
            }

            class GridViewHolder {
                ImageView iv;
            }
        }

        class BaseViewHolder {
            ImageView iv_avatar;
            TextView tv_name, tv_province, tv_school, tv_time, tv_content;
            LinearLayout ll_root;
        }

        class ImgsViewHolder extends BaseViewHolder {
            GridView gv;
        }

        class VoiceViewHolder extends BaseViewHolder {
            TextView tv_voice_time;
            ClickableSeekBar csb;
        }

        class EvaluateViewHolder {
            LinearLayout ll_container;
            ImageView iv_btn;
        }
    }

    private class VoiceTask extends TimerTask {
        @Override
        public void run() {
            if (mPlayingHolder != null && mPlayingHolder.csb != null && !isSliding) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String tag = (String) mPlayingHolder.csb.getTag();
                        if (mPlayingHolder != null && mPlayer.isPlaying() && mPlayingHolder
                                .voice_path.equals(tag)) {
                            int duration = mPlayer.getDuration();
                            int currentPosition = mPlayer.getCurrentPosition();
                            mPlayingHolder.csb.setProgress(((float) currentPosition) /
                                    duration);
                            mPlayingHolder.tv_voice_time.setText(Util.getFixedTime
                                    (currentPosition / 1000));
                        }
                    }
                });
            }
        }
    }

    @Override
    public void onEvent(MessageEvent event) {
        super.onEvent(event);
        if (MessageEvent.MessageType.ASK_DETAIL_RFRESH == event.messageType) {
            AskReceive askReceive = mGson.fromJson(event.message, AskReceive.class);
            if (askReceive != null && askReceive.askId.equals(mAskTeachers.askId)) {
                if (askReceive.type == 1) {
                    mList.remove(1);
                    mList.add(new Ask(AskDetailAdapter.TYPE_RESOLVE));
                    mAdapter.notifyDataSetChanged();
                } else if (askReceive.type == 2) {
                    isFirstComment = true;
                    notifyCommentList();
                }
            }
        }
    }

    private void notifyCommentList() {
        String requestUrl = UrlConstants.COMMENT_LIST + "&askid=" + mAskTeachers.askId;
        RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null && response.optBoolean("success") && response.optJSONArray
                        ("data") != null) {
                    List<Ask> comments = mGson.fromJson(response.optJSONArray("data").toString()
                            , new TypeToken<List<Ask>>() {
                    }.getType());
                    Ask mAccept = new Ask(AskDetailAdapter.TYPE_ACCEPT);
                    if (isFirstComment) {
                        mList.remove(1);
                        mList.addAll(comments);
                        mList.add(mAccept);
                        mAdapter.notifyDataSetChanged();
                        mLl_input.setVisibility(View.VISIBLE);
                        isFirstComment = false;
                        setCustomerOperationImageResourse(R.drawable.dot_more);
                        setCustomerOperationOnClickListener(AskDetailActivity.this);
                    } else {
                        Ask ask = mList.remove(0);
                        mList.clear();
                        mList.add(ask);
                        mList.addAll(comments);
                        mList.add(mAccept);
                        mAdapter.notifyDataSetChanged();
                        mEt_input.setText("");
                    }
                    mLv.setSelection(mAdapter.getCount() - 1);
                }
            }
        }, null, mTag);
    }
}
